package com.duoketech.controller.web.activity;

import com.duoketech.config.Constant;
import com.duoketech.controller.BaseController;
import com.duoketech.controller.web.ICRUD;
import com.duoketech.exception.ValidateException;
import com.duoketech.model.activity.Activity;
import com.duoketech.model.activityorder.ActivityOrder;
import com.duoketech.service.web.ModulePicService;
import com.duoketech.service.web.activity.ActivityService;
import com.duoketech.utils.PageUtil;
import com.duoketech.utils.upload.QiNiuUpload;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.upload.UploadFile;
import com.qiniu.common.QiniuException;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.List;

public class ActivityController extends BaseController implements ICRUD {

    @Override
    public void listAll() {
        int pageNum = getParaToInt("pageNum",1);
        int pageSize = getParaToInt("pageSize",10);
        Kv kv = Kv.by(Constant.MEMBER_ID_KEY,getShopMemberId());
        Page<Activity> result = (Page<Activity>) ActivityService.ME.pageAllRecord(getModel(Activity.class),pageNum,pageSize,kv);
        for (Activity each:result.getList()) {
            each.set(Activity.STATUS,each.status());
            each.set(Activity.START_TIME,PageUtil.formatDataYYMMDD(each.getDate(Activity.START_TIME)));
            each.set(Activity.END_TIME,PageUtil.formatDataYYMMDD(each.getDate(Activity.END_TIME)));
            each.set(Activity.PRICE,each.price());
        }
        renderObject(result);
    }

    @Override
    public void queryById() {
        int id = getParaToInt("id",0);
        try {
            Activity a = (Activity) ActivityService.ME.queryBy(id,getModel(Activity.class),Kv.create());
            a.set(Activity.PRICE,PageUtil.fengToYuan(a.getInt(Activity.PRICE)));
            a.set(Activity.START_TIME,PageUtil.formatDataYYMMDD(a.getDate(Activity.START_TIME)));
            a.set(Activity.END_TIME,PageUtil.formatDataYYMMDD(a.getDate(Activity.END_TIME)));
            setAttr("activity",a);
            render("edit-"+viewModel()+".html");
            } catch (ValidateException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void doAdd() {
        try {
            Activity activity = getModel(Activity.class);
            String price= getPara("price","1.0");
            activity.set(Activity.PRICE,PageUtil.yuanToFeng(Double.valueOf(price)));
            activity.set(Activity.CREATOR,getShopMemberId());
            ActivityService.ME.doAdd(activity,Kv.create());
            renderSuccess("操作成功");
            } catch (ValidateException e) {
            renderError(e.getMessage());
        }

    }

    @Override
    public void doUpdate() {
        try {
            Activity activity = getModel(Activity.class);
            String price= getPara("price","1.0");
            activity.set(Activity.PRICE,PageUtil.yuanToFeng(Double.valueOf(price)));
            activity.set(Activity.CREATOR,getShopMemberId());
            ActivityService.ME.update(activity,Kv.create());
            renderSuccess("操作成功");
            } catch (ValidateException e) {
            renderError(e.getMessage());
        }
    }

    @Override
    public void doDelete() {
        int id = getParaToInt("id",0);
        try {
        ActivityService.ME.deleteBy(id,getModel(Activity.class),Kv.create());
        renderSuccess("操作成功");
        } catch (ValidateException e) {
        renderError(e.getMessage());
        }
    }



    public void activityApply(){
        int id = getParaToInt("id",0);
        List<ActivityOrder> orders = ActivityService.ME.queryActivityOrderBy(id);
        renderObject(orders);
    }

    public void doSaveWxActivity(){
    	try {
    		Activity activity = getModel(Activity.class);
			ActivityService.ME.doSaveWxActivity(activity,getShopMemberId());
			renderSuccess("保存成功");
		} catch (ValidateException e) {
			renderError(e.getMessage());
		}
    	
    }
  }